using Identity.Application.Contracts.Common;
using Identity.Application.Contracts.Services;
using Identity.Application.Roles.Commands;
using MediatR;

namespace Identity.Application.Roles.CommandHandlers;

public class DeletRoleCommandHandler : IRequestHandler<DeleteRoleCommand, Result>
{
    private readonly IRoleService _roleService;

    public DeletRoleCommandHandler(IRoleService roleService)
    {
        _roleService = roleService;
    }
    public async Task<Result> Handle(DeleteRoleCommand request, CancellationToken cancellationToken)
    {
        var isExistRole = await _roleService.RoleExistsAsync(request.roleId);
        if (!isExistRole) return Result.Failure("角色不存在");
        
        var result = await _roleService.DeleteRoleAsync(request.roleId);

        if (!result.Succeeded) return Result.Failure(result.Error!);

        return Result.Success();
    }
}